/**
Copyright (c) 2008 Arnaud Fabre <arnaud333@gmail.com>

This file is part of Vos Tutos Offline.

Vos Tutos Offline is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Vos Tutos Offline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Vos Tutos Offline.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
File    : Alert.h
Purpose : Contains all message display by the application.
	  Privide a method for displaying messages : 
	  VTOAlert::alert(errno)
*/

#include "alert.h"

#include <QObject>
#include <QMessageBox>
#include <QMap>

int VTOAlert::alert(QString message, QWidget* parent)
{
    return QMessageBox::information(
            parent, QObject::tr("VTO Information"), message, QMessageBox::Ok );
}

int VTOAlert::alert(int errno, QWidget* parent)
{
    switch(getErrLvl(errno))
    {
    case 0:
	return QMessageBox::information(
	    parent, QObject::tr("VTO Information"), getErrText(errno), getErrButtons(errno));
	break;
    case 1:
	return QMessageBox::question( 
	    parent, QObject::tr("VTO Question"), getErrText(errno), QMessageBox::Yes | QMessageBox::No);
	break;		
    case 2:
	return QMessageBox::warning(
	    parent, QObject::tr("VTO Warning"), getErrText(errno), getErrButtons(errno));
	break;
    case 3:
	return QMessageBox::critical(
	    parent, QObject::tr("VTO Critical"), getErrText(errno), QMessageBox::Ok);
	break;
    default:
	return -1;
	break;		
    }
	
}

int VTOAlert::getErrLvl(int e)
{
    QMap<int, int> errLvl;
	
    // MainWindow Errors
    errLvl[101] = 3;
    errLvl[102] = 3;
    errLvl[103] = 3;
    errLvl[104] = 3;
    errLvl[105] = 3;
    errLvl[106] = 3;
	
    // MainWindow Warning
    errLvl[120] = 2;
	
    // Mdown Errors
    errLvl[200] = 3;
    errLvl[201] = 3;
	
    // Doc Errors	
    errLvl[300] = 3;
    errLvl[301] = 3;
	
    // File System Errors
    errLvl[400] = 3;
    errLvl[401] = 3;
    
    // Config Messages
    
    errLvl[500] = 0;

    // Tuto Modification Messages

    errLvl[600] = 0;
	
    return errLvl[e];	
}

QString VTOAlert::getErrText(int e)
{
    QMap<int, QString> errText;
	
    // MainWindow Errors
    errText[101] = QObject::tr("Can't save this document, report bug to <http://code.google.com/p/vto>");
    errText[102] = QObject::tr("Please select a Big Tutorial in the tuto tree before create a chapter");
    errText[103] = QObject::tr("Please select a chapter in the tuto tree before create a minituto");
    errText[104] = QObject::tr("Please select a mini tutorial in the tuto tree before create a part");
    errText[105] = QObject::tr("The selected item can't be moved in the tuto tree");
    errText[106] = QObject::tr("Can't delete the introduction or conclusion");
	
    // MainWindow Questions 
    errText[120] = QObject::tr("The document has been modified. Do you want to save before quit ?");
    // Mdown Errors
    errText[200] = QObject::tr("Mdown don't start, you need the mdown programm on your commputer. See <mdown home page>");
    errText[201] = QObject::tr("Mdown don't stop correctly");
	
    // Doc Errors
    errText[300] = QObject::tr("The xml document specified is not a VTO document");
    errText[301] = QObject::tr("The file specified is not a xml document");
	
    // File System Errors
    errText[400] = QObject::tr("The file can't be opened in Read only mode");
    errText[401] = QObject::tr("The file can't be opened in Write only mode");
    
	// Config Messages
    errText[500] = QObject::tr("The configuration has been saved");

        // Tuto modification Messages

    errText[600] = QObject::tr("The Licence has been updated");
    
    if(getErrLvl(e) == 3)
		return QString() + QObject::tr("Error n%1 : ").arg(e) + errText[e];
    else
		return errText[e];
}

QFlags<QMessageBox::StandardButton> VTOAlert::getErrButtons(int e)
{
    QMap<int, QFlags<QMessageBox::StandardButton> > errB;
    
    errB[120] = QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel;
    errB[500] = QMessageBox::Ok;
    errB[600] = QMessageBox::Ok;
    
	
    if(errB.contains(e)) return errB[e];
    else return QMessageBox::Ok;
}

